package com.xuecheng.basic.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.basic.domain.Category;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.web.exceptions.BusinessException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.basic.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 课程分类前端控制器
 * </p>
 *
 * @author 七安
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询课程分类目录
     *
     * @return 分类结果
     */
    @GetMapping("/tree-nodes")
    public ResponseResult<CategoryVo> findCourseType() {
        return ResponseResult.okResult(categoryService.findCourseType());
    }

    /**
     * 根据id查询分类名称
     *
     * @param id 要查询的id
     * @return 分类名称
     */
    @GetMapping("/{id}")
    public ResponseResult<CategoryVo> findSortName(@PathVariable("id") String id) throws BusinessException {
        Category category = categoryService.getById(id);
        //判空
        if (ObjectUtils.isEmpty(category)) {
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        //转成Vo
        CategoryVo categoryVo = BeanHelper.copyProperties(category, CategoryVo.class);
        return ResponseResult.okResult(categoryVo);
    }
}
